package com.atomgit.ai.proxy.controller;

import com.atomgit.ai.proxy.param.PromptParam;
import com.atomgit.ai.proxy.service.ModelInfoService;
import com.atomgit.ai.proxy.vo.ModelAnswerVO;
import com.atomgit.ai.proxy.vo.ModelInfoSelfVO;
import com.atomgit.ai.proxy.vo.ModelInfoVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/model")
public class ModelController {

    @Autowired
    private ModelInfoService modelInfoService;


    /**
     * 模型列表
     */
    @PostMapping("/list")
    public List<ModelInfoVO> list() {
        log.info("获取模型列表...");
        return modelInfoService.list();
    }


    /**
     * 获取已设置apikey的模型列表
     */
    @PostMapping("/list/self")
    public List<ModelInfoSelfVO> listSelf() {
        log.info("获取已设置apikey的模型列表...");
        return modelInfoService.listSelf();
    }

    /**
     * 根据prompt及历史记录获取指定模型回答
     */
    @PostMapping("/answer")
    public ModelAnswerVO answer(@RequestBody @Validated PromptParam param) throws ClassNotFoundException, InstantiationException, IllegalAccessException {
        return modelInfoService.answer(param);
    }

    /**
     * 根据prompt及历史记录获取指定模型回答并以流式返回结果
     */
    @PostMapping(value = "/answerInStream",consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
    public void answerInStream(@RequestBody @Validated PromptParam param, HttpServletResponse response) throws ClassNotFoundException, InstantiationException, IllegalAccessException, IOException {
        modelInfoService.answerInStream(param,response);
    }


}